package com.flower.subject.application.controller;

import com.flower.subject.entity.PageResult;
import com.flower.subject.infra.basic.entity.EsSubjectFields;
import com.flower.subject.infra.basic.entity.SubjectInfoEs;
import com.flower.subject.infra.basic.es.EsIndexInfo;
import com.flower.subject.infra.basic.es.EsRestClient;
import com.flower.subject.infra.basic.es.EsSearchRequest;
import com.flower.subject.infra.basic.service.SubjectEsService;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-07-26 13:46
 */
@RestController
@RequestMapping("es")
public class EsController {

    @Resource
    private SubjectEsService service;


    @GetMapping("/add")
    private void add(){

    }


    @GetMapping("/find")
    private PageResult<SubjectInfoEs>  find(){
        SubjectInfoEs subjectInfoEs = new SubjectInfoEs();
        subjectInfoEs.setKeyWord("vue");
        PageResult<SubjectInfoEs> subjectInfoEsPageResult = service.querySubjectList(subjectInfoEs);

        return subjectInfoEsPageResult;
    }



    @GetMapping("/find2")
    private SearchResponse  find2(){

        EsSearchRequest esSearchRequest = new EsSearchRequest();
        BoolQueryBuilder bq = new BoolQueryBuilder();
        MatchQueryBuilder subjectNameBuildreqer = QueryBuilders.matchQuery(EsSubjectFields.SUBJECT_NAME, "vue");
        bq.should(subjectNameBuildreqer);


        // 权重
        subjectNameBuildreqer.boost(2);
        MatchQueryBuilder subjectAnswerBuildreqer = QueryBuilders.matchQuery(EsSubjectFields.SUBJECT_ANSWER, "vue");
        bq.should(subjectAnswerBuildreqer);

        MatchQueryBuilder subjectTypeBuildreqer = QueryBuilders.matchQuery(EsSubjectFields.SUBJECT_TYPE, 1);
        bq.must(subjectTypeBuildreqer);
        bq.minimumShouldMatch(1);


        // 高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder().field("*").requireFieldMatch(false);
        highlightBuilder.preTags("<span style = \"color:red\">");
        highlightBuilder.postTags("</span>");

        esSearchRequest.setBq(bq);
        esSearchRequest.setHighlightBuilder(highlightBuilder);
        esSearchRequest.setFields(EsSubjectFields.FIELD_QUERY);
        // 分页

        esSearchRequest.setNeedScroll(false);

        SearchResponse searchResponse = EsRestClient.searchTermQuery(getEsIndexInfo(), esSearchRequest);

        System.out.println(esSearchRequest);
        return searchResponse;
    }


    private EsIndexInfo getEsIndexInfo() {
        EsIndexInfo esIndexInfo = new EsIndexInfo();
        esIndexInfo.setClusterName("test");
        esIndexInfo.setIndexName("subject_index");
        return esIndexInfo;
    }
}
